package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.business.domain.TPayChannel;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.mapper.TPayChannelMapper;
import com.ruoyi.business.service.ITPayChannelService;
import com.ruoyi.business.service.ITRecipientInfoService;
import com.ruoyi.business.service.ITUserChannelService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 支付通道Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class TPayChannelServiceImpl implements ITPayChannelService 
{
    @Autowired
    private TPayChannelMapper tPayChannelMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITUserChannelService userChannelService;

    @Autowired
    private ITRecipientInfoService recipientInfoService;

    /**
     * 查询支付通道
     * 
     * @param id 支付通道主键
     * @return 支付通道
     */
    @Override
    public TPayChannel selectTPayChannelById(Long id)
    {
        return tPayChannelMapper.selectTPayChannelById(id);
    }

    /**
     * 查询支付通道列表
     * 
     * @param tPayChannel 支付通道
     * @return 支付通道
     */
    @Override

    public List<TPayChannel> selectTPayChannelList(TPayChannel tPayChannel)
    {
        return tPayChannelMapper.selectTPayChannelList(tPayChannel);
    }

    @Override
    public List<TPayChannel> selectTPayChannelListByUser(TPayChannel tPayChannel) {
        return tPayChannelMapper.selectTPayChannelListByUser(tPayChannel);
    }

    /**
     * 新增支付通道
     * 
     * @param tPayChannel 支付通道
     * @return 结果
     */
    @Override
    public int insertTPayChannel(TPayChannel tPayChannel)
    {
        tPayChannel.setCreateTime(DateUtils.getNowDate());
        tPayChannelMapper.insertTPayChannel(tPayChannel);
//        boolean isAdmin = ShiroUtils.getSysUser().isAdmin();
//        //为代理和码商添加通道
//        if(isAdmin){
//            SysUser query = new SysUser();
//            List<SysUser> sysUsers = sysUserService.selectUserList(query);
//            for(SysUser sysUser:sysUsers){
//                TUserChannel userChannel = new TUserChannel();
//                userChannel.setChannel(tPayChannel.getChannel());
//                userChannel.setUserId(sysUser.getUserId());
//                userChannel.setUserRate(0);
//                userChannel.setDisabled(0);
//                userChannel.setType(tPayChannel.getType());
//                userChannelService.insertTUserChannel(userChannel);
//            }
//        }
        return 1;
    }

    /**
     * 修改支付通道
     * 
     * @param tPayChannel 支付通道
     * @return 结果
     */
    @Override
    public int updateTPayChannel(TPayChannel tPayChannel)
    {
        tPayChannel.setUpdateTime(DateUtils.getNowDate());
        tPayChannelMapper.updateTPayChannel(tPayChannel);
        //修改相应账号的登录方式
        TRecipientInfo recipientInfoParam = new TRecipientInfo();
        recipientInfoParam.setChannel(tPayChannel.getChannel());
        List<TRecipientInfo> recipientInfoList =  recipientInfoService.selectTRecipientInfoList(recipientInfoParam);
        for(TRecipientInfo recipientInfoResult:recipientInfoList){
            TRecipientInfo recipientInfo = new TRecipientInfo();
            BeanUtil.copyProperties(recipientInfoResult, recipientInfo);
            recipientInfo.setLoginType(tPayChannel.getLoginType());
            recipientInfo.setNeedOnline(tPayChannel.getNeedOnLine());
            recipientInfo.setChannelDisabled(tPayChannel.getDisabled());
            recipientInfoService.updateTRecipientInfo(recipientInfo);
        }

        return 1;
    }

    /**
     * 批量删除支付通道
     * 
     * @param ids 需要删除的支付通道主键
     * @return 结果
     */
    @Override
    public int deleteTPayChannelByIds(String ids)
    {
        return tPayChannelMapper.deleteTPayChannelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除支付通道信息
     * 
     * @param id 支付通道主键
     * @return 结果
     */
    @Override
    public int deleteTPayChannelById(Long id)
    {
        return tPayChannelMapper.deleteTPayChannelById(id);
    }

    @Override
    public TPayChannel selectTPayChannelByCode(String code) {
        TPayChannel query = new TPayChannel();
        query.setChannel(code);
        return tPayChannelMapper.selectTPayChannelList(query).get(0);
    }
}
